﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using U_UserHome.Services.Home;
using System.Runtime.CompilerServices;
using DynamicLoader;
namespace U_UserHome
{
    public partial class UserHomePage : UserControl
    {
        public UserHomePage()
        {
            InitializeComponent();
            SHome.GetBasicMembersCompleted += new EventHandler<GetBasicMembersCompletedEventArgs>(SHome_GetBasicMembersCompleted);
            SHome.GetBasicMembersAsync();
        }

        void SHome_GetBasicMembersCompleted(object sender, GetBasicMembersCompletedEventArgs e)
        {
            foreach (var Member in e.Result)
            {
                Member.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Member_PropertyChanged);
                foreach (var Rack in Member.Racks)
                {
                    Rack.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Rack_PropertyChanged);
                }
            }
            RenewsChecked = e.Result.Count((M) => M.RenewChecked == true);
            UpdateRenew();
            Members.ItemsSource = e.Result;
        }

        void Rack_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var s = sender as BasicRackAllocation;
            SHome.SetRenewCheckedForRackAllocationAsync(s.ID, s.RenewChecked);
        }

        void Member_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var s = sender as BasicMember;
            RenewsChecked += (s.RenewChecked == true) ? 1 : -1;
            UpdateRenew();
            SHome.SetRenewCheckedForMemberAsync(s.ID, s.RenewChecked);
        }
        void UpdateRenew()
        {
            btnRenew.IsEnabled = (RenewsChecked != 0);
        }
        int RenewsChecked = 0;


        UserHomeClient SHome = ServiceConfig.NewUserHomeClient();

        private void Renew(object sender, RoutedEventArgs e)
        {
            SSCCMembership.BusyIndicator.Show();
            XAPLoader XAPL = new XAPLoader(new SSCCMembership.ExternalDependanciesLoader(), "PrintScreens", 0, new DynamicLoader.DataStructures.XAPName[] { });
            XAPL.DownloadComplete += new EventHandler<DownloadCompleteEventArgs>(XAPL_DownloadComplete);
            XAPL.Start();
        }

        void XAPL_DownloadComplete(object sender, DownloadCompleteEventArgs e)
        {
            e.Stream.LoadAssembly("PrintScreens");
            RenewNow();
        }
        [MethodImpl(MethodImplOptions.NoInlining)]
        void RenewNow()
        {
            SSCCMembership.BusyIndicator.Hide();
            PrintScreens.RenewalSummaryScreen RSS = new PrintScreens.RenewalSummaryScreen();
            RSS.ParentLayoutRoot = LayoutRoot;
            RSS.ShowDialog();
        }
    }

}
